//
//  GC_SelectionCriteriaSegmentView.m
//  OnlineRetailers
//
//  Created by mini3 on 2017/6/6.
//  Copyright © 2017年 OnlineRetailers. All rights reserved.
//

#import "GC_SelectionCriteriaSegmentView.h"


@interface GC_SelectionCriteriaSegmentView ()
///按钮
@property(nonatomic, strong) NSMutableArray *buttonArray;
///箭头
@property(nonatomic, strong) NSMutableArray *arrowArray;

///高度
@property(nonatomic, assign) CGFloat viewHeight;

///上分界线
@property(nonatomic, weak) UIImageView *topLineImage;
///下分界线
@property(nonatomic, weak) UIImageView *bottomLineImage;
///默认字体
@property(nonatomic, strong) UIFont *defaultFont;
///默认字体颜色
@property(nonatomic, strong) UIColor *defalutFontColor;
///选中字体
@property(nonatomic, strong) UIFont *selectFont;
///选中字体颜色
@property(nonatomic, strong) UIColor *selectFontColor;


//是否变化
@property (nonatomic, assign) BOOL isVariety;


@property (nonatomic, assign) NSInteger curreateItem;
@end

@implementation GC_SelectionCriteriaSegmentView

///初始化
-(id)initWithHeight:(CGFloat)height
{
    self = [super init];
    if(self){
        [self initDefaultWithHeight:height];
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

///初始
-(void)initDefaultWithHeight:(CGFloat)height
{
    self.viewHeight = height;
    self.frame = CGRectMake(0, 0, kMainScreenWidth, height);
    self.backgroundColor = kCommonWhiteBg;
    
    
    self.defaultFont = kFontSize_28;
    self.defalutFontColor = kFontColorBlack;
    self.selectFont = kFontSize_28;
    self.selectFontColor = kFontColorRed;
    
    [self loadSubViewAndConstraints];
}

///加载子视图及约束
-(void)loadSubViewAndConstraints
{
    [self.topLineImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.centerX.equalTo(self);
        make.top.equalTo(self);
    }];
    
    [self.bottomLineImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self);
        make.centerX.equalTo(self);
        make.bottom.equalTo(self);
    }];
}


///设置item
-(void)setUnLineItems:(NSArray*)nameArray
{
    if(nameArray.count <= 0){
        return;
    }
    self.isVariety = NO;
    
    //清除
    for(UIButton* button in self.buttonArray){
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    
    for (UIImageView *image in self.arrowArray) {
        [image removeFromSuperview];
    }
    [self.arrowArray removeAllObjects];
    
    
    //按钮宽
    CGFloat buttonWidth = self.frame.size.width / nameArray.count;
    
    for(NSInteger i = 0; i < nameArray.count; i++){
        //按钮
        UIButton *button = [UIButton createNoBgButtonWithTitle:[nameArray objectAtIndex:i] target:self action:@selector(onButtonAction:)];
        button.tag = i;
        button.frame = CGRectMake(buttonWidth*i, 0, buttonWidth, self.frame.size.height);
        [button setTitleClor:self.defalutFontColor];
        [button setTitleColor:self.selectFontColor forState:UIControlStateSelected];
        button.titleLabel.font = self.defaultFont;
        [self addSubview:button];
        [self.buttonArray addObject:button];
        if (i == 4) {
            [button setTitle:@"" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:[nameArray objectAtIndex:i]] forState:UIControlStateNormal];
        }
        if(self.updateNum == 2){
            //箭头
            UIImageView *image = [UIImageView createImageViewWithName:@"public_arrow"];
            if (i == 0){
                [image setImageForName:@""];
            }
            if(i == 1){
//                [image setImageForName:@""];
                 [image setImageForName:@"public_arrow33"];
            }
            if(i == 2){
                [image setImageForName:@""];
            }
            if(i == 3){
                [image setImageForName:@""];
            }
            if (i == 4) {
                [image setImageForName:@""];
            }
            image.tag = i;
            [self addSubview:image];
            [image mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(button);
                make.left.equalTo(button.titleLabel.mas_right).offset(WIDTH_TRANSFORMATION(10));
            }];
            [self.arrowArray addObject:image];
        }else if(self.updateNum == 1){
            
            //箭头
            UIImageView *image = [UIImageView createImageViewWithName:@"public_arrow"];
            if(i == 1){
                [image setImageForName:@""];
            }
            image.tag = i;
            [self addSubview:image];
            [image mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(button);
                make.left.equalTo(button.titleLabel.mas_right).offset(WIDTH_TRANSFORMATION(10));
            }];
            [self.arrowArray addObject:image];
        }else{
            //箭头
            UIImageView *image = [UIImageView createImageViewWithName:@"public_arrow"];
            image.tag = i;
            [self addSubview:image];
            [image mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(button);
                make.left.equalTo(button.titleLabel.mas_right).offset(WIDTH_TRANSFORMATION(10));
            }];
            [self.arrowArray addObject:image];
        }
    }
}


///修改 按钮字体标题
- (void)setButtonTitleWith:(NSString *)title andItem:(NSInteger)item
{
    UIButton *button = ((UIButton *)[self.buttonArray objectAtIndex:item]);
    [button setTitle:title];
}


///设置选中项
-(void)setSelectItem:(NSInteger)item
{
    if(item < 0 || item >= self.buttonArray.count){
        return;
    }
    
    
    //全不选中
    for (UIButton *button in self.buttonArray) {
        [button setSelected:NO];
    }
    
    NSInteger i = 0;
    for (UIImageView *image in self.arrowArray) {
        [image setImageForName:@"public_arrow"];
        
        if(self.updateNum == 1){
            if(i==1){
                [image setImageForName:@""];
            }
        }else if(self.updateNum == 2){
            if(i==1){
//                [image setImageForName:@""];
                [image setImageForName:@"public_arrow33"];
            }else if(i == 3){
                [image setImageForName:@""];
            }
            else if(i == 2){
                [image setImageForName:@""];
            }
            else if(i == 0){
                [image setImageForName:@""];
            }
            else if (i == 4){
                [image setImageForName:@""];
            }
            
            
            
        }
        
        i++;
    }
    
    if(self.updateNum == 1){
        if(item == 1){
//            if(self.isVariety){
//                self.isVariety = NO;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow1"];
//            }else{
//                self.isVariety = YES;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow3"];
//            }
            
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }else{
            self.isVariety = NO;
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@"public_arrow3"];
        }
        
    }else if(self.updateNum == 2){
//        if(item == 1 && self.curreateItem == 2){
//            self.isVariety = NO;
//        }else if(item == 2 && self.curreateItem == 1){
//            self.isVariety = NO;
//        }

        if(item == 2 && self.curreateItem == 1){
            self.isVariety = NO;
        }
//        item == 1 ||
        if(item == 1){
           
            if(self.isVariety){
                self.isVariety = NO;
                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
                [image setImageForName:@"public_arrow32"];
            }else{
                self.isVariety = YES;
                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
                [image setImageForName:@"public_arrow31"];
            }
        }else if(item == 3){
//            if(self.isVariety){
//                self.isVariety = NO;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow32"];
//            }else{
//                self.isVariety = YES;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow31"];
//            }
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }else{
            self.isVariety = NO;
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }
    }else{
        HenLog(@"%ld",(long)self.updateNum);
        self.isVariety = NO;
        UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
        [image setImageForName:@""];
    }
    
    self.curreateItem = item;
    
    
    UIButton *button = ((UIButton *)[self.buttonArray objectAtIndex:item]);
    [button setSelected:YES];
    
    
    
    
    if(self.onSelectedBlock){
        self.onSelectedBlock(item, self.isVariety);
    }
}

///设置默认选中项
-(void)setSelectDefaultItem:(NSInteger)item
{
    if(item < 0 || item >= self.buttonArray.count){
        return;
    }
    //全不选中
    for (UIButton *button in self.buttonArray) {
        [button setSelected:NO];
    }
    
    NSInteger i = 0;
    for (UIImageView *image in self.arrowArray) {
        [image setImageForName:@"public_arrow"];
        
        if(self.updateNum == 1){
            if(i==1){
                [image setImageForName:@""];
            }
        }else if(self.updateNum == 2){
            if(i==1){
//                [image setImageForName:@""];
                  [image setImageForName:@"public_arrow33"];
            }else if(i == 3){
//                [image setImageForName:@"public_arrow33"];
                 [image setImageForName:@""];
            }else if(i == 0){
                [image setImageForName:@""];
            }else if(i == 2){
                [image setImageForName:@""];
            }else if (i == 4){
                [image setImageForName:@""];
            }
        }
        
        i++;
    }
    
    if(self.updateNum == 1){
        if(item == 1){
            //            if(self.isVariety){
            //                self.isVariety = NO;
            //                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            //                [image setImageForName:@"public_arrow1"];
            //            }else{
            //                self.isVariety = YES;
            //                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            //                [image setImageForName:@"public_arrow3"];
            //            }
            
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }else{
            self.isVariety = NO;
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }
        
    }else if(self.updateNum == 2){
        //        if(item == 1 && self.curreateItem == 2){
        //            self.isVariety = NO;
        //        }else if(item == 2 && self.curreateItem == 1){
        //            self.isVariety = NO;
        //        }
        
        if(item == 2 && self.curreateItem == 1){
            self.isVariety = NO;
        }
        //        item == 1 ||
        if(item == 1){
//            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//            [image setImageForName:@""];
            if(self.isVariety){
                self.isVariety = NO;
                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
                [image setImageForName:@"public_arrow32"];
            }else{
                self.isVariety = YES;
                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
                [image setImageForName:@"public_arrow31"];
            }
        }else if(item == 2){
//            if(self.isVariety){
//                self.isVariety = NO;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow32"];
//            }else{
//                self.isVariety = YES;
//                UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
//                [image setImageForName:@"public_arrow31"];
//            }
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }else{
            self.isVariety = NO;
            UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
            [image setImageForName:@""];
        }
    }else{
        HenLog(@"%ld",(long)self.updateNum);
        self.isVariety = NO;
        UIImageView *image = ((UIImageView *)[self.arrowArray objectAtIndex:item]);
        [image setImageForName:@"public_arrow3"];
    }
    
    self.curreateItem = item;
    
    
    UIButton *button = ((UIButton *)[self.buttonArray objectAtIndex:item]);
    [button setSelected:YES];
}


///设置上分界线显隐
-(void)setTopLineImageViewHidden:(BOOL)hidden
{
    self.topLineImage.hidden = hidden;
}
///设置下分界线显隐
-(void)setBottomLineImageViewHidden:(BOOL)hidden
{
    self.bottomLineImage.hidden = hidden;
    
}

#pragma mark -- event response

-(void)onButtonAction:(UIButton*)sender
{
    
    [self setSelectItem:sender.tag];
}

#pragma mark -- getter, setter

///按钮
-(NSMutableArray*)buttonArray
{
    if(!_buttonArray){
        _buttonArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _buttonArray;
}


///箭头
- (NSMutableArray *)arrowArray
{
    if(!_arrowArray){
        _arrowArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _arrowArray;
}

///上分界线
- (UIImageView *)topLineImage
{
    if(!_topLineImage){
        UIImageView *image = [UIImageView createImageViewWithName:@"public_line"];
        [self addSubview:_topLineImage = image];
    }
    return _topLineImage;
}

///下分界线
- (UIImageView *)bottomLineImage
{
    if(!_bottomLineImage){
        UIImageView *image = [UIImageView createImageViewWithName:@"public_line"];
        [self addSubview:_bottomLineImage = image];
    }
    return _bottomLineImage;
}
@end
